package top.infopub.job.hr.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.job.hr.dao.NcOrginfoDao;
import top.infopub.job.hr.service.INcOrginfoService;
import top.infopub.job.util.NCUtil;
import top.infopub.model.hr.NcOrginfo;
import top.infopub.util.DateUtil;


/**
 * NC系统组织信息Service业务层处理
 *
 * @author zxy
 * @date 2020-12-21
 */
@Service
public class NcOrginfoServiceImpl extends GenericServiceImpl<NcOrginfo, String> implements INcOrginfoService
{

    @Autowired
    private NcOrginfoDao ncOrginfoDao;


    @Override
    public GenericDao<NcOrginfo, String> getDao() {
        return ncOrginfoDao;
    }

    /**
     * 查询NC系统组织信息
     *
     * @param pkOrg NC系统组织信息ID
     * @return NC系统组织信息
     */
    @Override
    public NcOrginfo selectNcOrginfoById(String pkOrg)
    {
        List<NcOrginfo> orginfos = ncOrginfoDao.selectNcOrginfoById(pkOrg);
        if(orginfos!=null){
            return orginfos.get(0);
        }
        return null;
    }

    /**
     * 查询NC系统组织信息列表
     *
     * @param ncOrginfo NC系统组织信息
     * @return NC系统组织信息
     */
    @Override
    public List<NcOrginfo> selectNcOrginfoList(NcOrginfo ncOrginfo)
    {
        return ncOrginfoDao.selectNcOrginfoList(ncOrginfo);
    }

    /**
     * 新增NC系统组织信息
     *
     * @param ncOrginfo NC系统组织信息
     * @return 结果
     */
    @Override
    public int insertNcOrginfo(NcOrginfo ncOrginfo)
    {
        return ncOrginfoDao.insertNcOrginfo(ncOrginfo);
    }

    /**
     * 修改NC系统组织信息
     *
     * @param ncOrginfo NC系统组织信息
     * @return 结果
     */
    @Override
    public int updateNcOrginfo(NcOrginfo ncOrginfo)
    {
        return ncOrginfoDao.updateNcOrginfo(ncOrginfo);
    }

    /**
     * 批量删除NC系统组织信息
     *
     * @param pkOrgs 需要删除的NC系统组织信息ID
     * @return 结果
     */
    @Override
    public int deleteNcOrginfoByIds(String[] pkOrgs)
    {
        return ncOrginfoDao.deleteNcOrginfoByIds(pkOrgs);
    }

    /**
     * 删除NC系统组织信息信息
     *
     * @param pkOrg NC系统组织信息ID
     * @return 结果
     */
    @Override
    public int deleteNcOrginfoById(String pkOrg)
    {
        return ncOrginfoDao.deleteNcOrginfoById(pkOrg);
    }


    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    @Transactional
    public void initNcOrgInfoTEMP() {
        String start = DateUtil.currentDateDef();
        log.info("执行--+++——-------initNcOrgInfoTEMP开始时间：{}", start);
        ncOrginfoDao.deleteNcOrginfoAll();
        ncOrginfoDao.deleteNcOrginfoAllTEMP();
        boolean flag=true;
        int n=1;
        while (flag){
            JSONObject sr = NCUtil.getOrgInfo(n+"","500",null,null,null);
            JSONArray data = sr.getJSONArray("data");
            if(data!=null&&data.size()>0){
                flag=true;
                n++;
                List<NcOrginfo> list = JSONArray.parseArray(data.toString(),NcOrginfo.class);
                ncOrginfoDao.batchInsertNcOrginfoTEMP(list);
            }else{
                flag=false;
            }
        }
        ncOrginfoDao.insertRromTEMP();
        String end = DateUtil.currentDateDef();
        log.info("执行--+++——-------initNcOrgInfoTEMP结束时间：{}", end);
    }

    @Override
    @Transactional
    public void initNcOrgInfo() {
        String start = DateUtil.currentDateDef();
        log.info("执行--+++——-------initNcOrgInfo开始时间：{}", start);
        ncOrginfoDao.deleteNcOrginfoAll();

        //获取大建筑组织
        JSONObject sr = NCUtil.getOrgInfo("1", "3000", "0001B210000000F8UPUN", null, null);
        JSONArray data = sr.getJSONArray("data");
        List<NcOrginfo> firstNcOrgList = JSONArray.parseArray(data.toString(),NcOrginfo.class);
        if(firstNcOrgList!=null&&firstNcOrgList.size()>0){
            ncOrginfoDao.batchInsertNcOrginfo(firstNcOrgList);
        }
        boolean flag=true;
        List<NcOrginfo> orginfos=new ArrayList<>();
        orginfos=firstNcOrgList;
        while (flag){
            orginfos= getOrgList(orginfos);
            if(orginfos.size()<=0){
                flag=false;
            }
        }

        String end = DateUtil.currentDateDef();
        log.info("执行--+++——-------initNcOrgInfo结束时间：{}", end);
    }


    public List<NcOrginfo> getOrgList(List<NcOrginfo> list){
        List<NcOrginfo> all=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                JSONObject sonStr = NCUtil.getOrgInfo("1", "5000", null, list.get(i).getPkOrg(), null);
                JSONArray sonData = sonStr.getJSONArray("data");
                if(sonData!=null&&sonData.size()>0){
                    List<NcOrginfo> sonNcOrgList = JSONArray.parseArray(sonData.toString(),NcOrginfo.class);
                    ncOrginfoDao.batchInsertNcOrginfo(sonNcOrgList);
                    all.addAll(sonNcOrgList);
                }
            }
        }
        return all;
    }
}
